package cn.com.basic.framework.exception.exceptions;

import cn.com.basic.framework.exception.enums.IErrorCode;
import cn.com.basic.framework.exception.enums.errors.SystemError;
import lombok.Data;

/**
 * 系统异常
 * @author cuixk
 */
@Data
public class SysException extends RuntimeException {

    private final IErrorCode error;
    private final String msg;

    private SysException(IErrorCode error) {
        super(String.format("Code:[%s],Desc:[%s]", error.getKey(), error.getValue()));
        this.error = error;
        this.msg = null;
    }

    private SysException(IErrorCode error, String msg) {
        super(String.format("Code:[%s],Desc:[%s],Msg:[%s]", error.getKey(), error.getValue(), msg));
        this.error = error;
        this.msg = msg;
    }

    private SysException(IErrorCode error, Throwable e) {
        super(String.format("Code:[%s],Desc:[%s],Msg:[%s]", error.getKey(), error.getValue(), e.getMessage()));
        this.error = error;
        this.msg = e.getMessage();
    }

    public static SysException newException(IErrorCode error) {
        return new SysException(error);
    }

    public static SysException newException(IErrorCode error, String msg) {
        return new SysException(error, msg);
    }

    public static SysException newException(IErrorCode error, Throwable e) {
        return new SysException(error, e);
    }

}
